package vsvm.gui.forms;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import vsvm.data.filter.NormalizeAttributeFilter;

public class ConfigureNormalizeFilterDialog extends JDialog {

	private JPanel jContentPane = null;
	private JPanel pnlButtons = null;
	private JPanel pnlCenter = null;
	private JButton btnOk = null;
	private JButton btnCancel = null;
	private JLabel lblMin = null;
	private JLabel lblMex = null;
	private JTextField txtMin = null;
	private JTextField txtMax = null;
	
	private NormalizeAttributeFilter filter;
	
	/**
	 * This method initializes pnlButtons	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnlButtons() {
		if (pnlButtons == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setHgap(15);
			flowLayout.setVgap(10);
			pnlButtons = new JPanel();
			pnlButtons.setLayout(flowLayout);
			pnlButtons.setPreferredSize(new java.awt.Dimension(45,45));
			pnlButtons.add(getBtnOk(), null);
			pnlButtons.add(getBtnCancel(), null);
		}
		return pnlButtons;
	}

	/**
	 * This method initializes pnlCenter	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getPnlCenter() {
		if (pnlCenter == null) {
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints3.gridy = 1;
			gridBagConstraints3.weightx = 1.0;
			gridBagConstraints3.weighty = 1.0;
			gridBagConstraints3.insets = new java.awt.Insets(10,10,10,10);
			gridBagConstraints3.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints3.gridx = 1;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints2.gridy = 0;
			gridBagConstraints2.weightx = 1.0;
			gridBagConstraints2.weighty = 1.0;
			gridBagConstraints2.insets = new java.awt.Insets(10,10,10,10);
			gridBagConstraints2.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints2.gridx = 1;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.insets = new java.awt.Insets(10,10,10,10);
			gridBagConstraints1.weightx = 0.0;
			gridBagConstraints1.weighty = 1.0;
			gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints1.gridy = 1;
			lblMex = new JLabel();
			lblMex.setText("Max value");
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.insets = new java.awt.Insets(10,10,10,10);
			gridBagConstraints.weightx = 0.0;
			gridBagConstraints.weighty = 1.0;
			gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints.gridy = 0;
			lblMin = new JLabel();
			lblMin.setText("Min value");
			pnlCenter = new JPanel();
			pnlCenter.setLayout(new GridBagLayout());
			pnlCenter.add(lblMin, gridBagConstraints);
			pnlCenter.add(lblMex, gridBagConstraints1);
			pnlCenter.add(getTxtMin(), gridBagConstraints2);
			pnlCenter.add(getTxtMax(), gridBagConstraints3);
		}
		return pnlCenter;
	}

	/**
	 * This method initializes btnOk	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnOk() {
		if (btnOk == null) {
			btnOk = new JButton();
			btnOk.setPreferredSize(new java.awt.Dimension(80,25));
			btnOk.setText("OK");
			btnOk.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					try {
						Double.parseDouble(txtMin.getText());			
					} catch (NumberFormatException ex) {
						JOptionPane.showMessageDialog(ConfigureNormalizeFilterDialog.this, "Min value must be number.", "Error", JOptionPane.ERROR_MESSAGE);
						return;
					}
					
					try {
						Double.parseDouble(txtMax.getText());			
					} catch (NumberFormatException ex) {
						JOptionPane.showMessageDialog(ConfigureNormalizeFilterDialog.this, "MAx value size must be number.", "Error", JOptionPane.ERROR_MESSAGE);
						return;
					}
					
					filter.setMin(Double.parseDouble(txtMin.getText()));
					filter.setMax(Double.parseDouble(txtMax.getText()));
					
					dispose();
				}
			});
		}
		return btnOk;
	}

	/**
	 * This method initializes btnCancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCancel() {
		if (btnCancel == null) {
			btnCancel = new JButton();
			btnCancel.setText("Cancel");
			btnCancel.setPreferredSize(new java.awt.Dimension(80,25));
			btnCancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					dispose();
				}
			});
		}
		return btnCancel;
	}

	/**
	 * This method initializes txtMin	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtMin() {
		if (txtMin == null) {
			txtMin = new JTextField();
		}
		return txtMin;
	}

	/**
	 * This method initializes txtMax	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTxtMax() {
		if (txtMax == null) {
			txtMax = new JTextField();
		}
		return txtMax;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	/**
	 * This is the default constructor
	 */
	public ConfigureNormalizeFilterDialog(Frame parent, NormalizeAttributeFilter f) {
		super(parent);
		initialize();
		setModal(true);
		setLocationRelativeTo(parent);
		filter = f;
		
		loadParams();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(304, 156);
		this.setTitle("Configure normalize filter");
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getPnlButtons(), java.awt.BorderLayout.SOUTH);
			jContentPane.add(getPnlCenter(), java.awt.BorderLayout.CENTER);
		}
		return jContentPane;
	}
	
	private void loadParams() {
		txtMin.setText(Double.toString(filter.getMin()));
		txtMax.setText(Double.toString(filter.getMax()));
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
